package com.bestdata.em.datasyn.datareceive.service;

import com.alibaba.fastjson.JSONObject;
import com.bestdata.em.datasyn.datareceive.model.DrsModelPOJO;
import com.bestdata.em.datasyn.datareceive.model.RawData;
import com.bestdata.em.datasyn.queue.MsgQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * 数据转发前封装处理核心方法
 * @author chizhenyu
 * @date 2020年8月28日10:49:38
 */
@Component
@Service
//@DependsOn("springContextHolder")
public class DataProcessService extends Thread{

    private Thread thread;

    @Value("${correspond.data}")
    private String sns;

    private String hid = "GNSS";

    private HashMap<String, String> sMap = new HashMap<>();

    @PostConstruct
    public void init(){
        //todo 可尝试使用线程池
        for (int i = 0; i < 1; i++) {
            this.thread = new Thread(this);
            this.thread.start();
        }
    }

    @Override
    public void run(){

        //解析测点与SN的对应关系
        String[] split = sns.split(",");

        for (int i = 0; i < split.length; i++) {

            String[] sn = split[i].split(":");

            sMap.put(sn[0],sn[1]);
        }

        while (true){
            RawData data = new RawData();

            try {
                data = MsgQueue.Queue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            String devSn = sMap.get(data.getStationName());

            DrsModelPOJO pojo = new DrsModelPOJO();
            LinkedHashMap<String,String> dMap = new LinkedHashMap<>();

            //设备没有相匹配的SN跳过循环
            if(StringUtils.isEmpty(devSn)){
                continue;
            }

            pojo.setSn(devSn);
            pojo.setHid(hid);
            pojo.setTm(data.getTime()+"");

            //分别拼写XYH的通道编码
            if(!StringUtils.isEmpty(data.getX())){
                String paraid = "010001"+data.getStationName();
                dMap.put(paraid,data.getX());
            }
            if(!StringUtils.isEmpty(data.getY())){
                String paraid = "020001"+data.getStationName();
                //Y轴方向取相反数
                dMap.put(paraid,(-Double.parseDouble(data.getY()))+"");
            }
            if(!StringUtils.isEmpty(data.getH())){
                String paraid = "030001"+data.getStationName();
                //H轴方向取相反数
                dMap.put(paraid,(-Double.parseDouble(data.getH()))+"");
            }

            pojo.setData(dMap);

            //todo 封装转发实体类
            try {
                //拼装数据发送DRS
                NewMessageSend.newMessageSendCore(JSONObject.toJSONString(pojo));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }



}
